package org.wendu.wdoa.api;


import org.springframework.web.bind.annotation.RequestHeader;
import org.wendu.wdoa.common.CurrentUser;
import org.wendu.wdoa.common.OaConstants;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.common.token.TokenUser;
import org.wendu.wdoa.common.token.TokenUtils;
import org.wendu.wdoa.dto.MenuDto;
import org.wendu.wdoa.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /*
     * 登录
     */
//    @GetMapping("/menu")//@AuthenticationPrincipal 说明参数是SpringSecurity当前用户
//    public Result<List<MenuDto>> userMenuList(@AuthenticationPrincipal CurrentUser currentUser, HttpServletRequest request){
//        //CurrentUser currentUser =(CurrentUser) session.getAttribute(OaConstants.SESSION_ATTR_NAME_CURRENT_USER);
//        List<MenuDto> menuList = homeService.getMenuList(currentUser.getUserId());
//        return Result.OK(menuList);


    @GetMapping("/menu")
    public Result<List<MenuDto>> userMenuList(@RequestHeader(OaConstants.HEADER_NAME_TOKEN) String token){

        TokenUser tokenUser = TokenUtils.getTokenUser(token);

        List<MenuDto> menuList = homeService.getMenuList(tokenUser.getUserId());
        return Result.OK(menuList);
    }


    @GetMapping("/mee")
    public Result<CurrentUser> currentUser(@RequestHeader(OaConstants.HEADER_NAME_TOKEN) String token){
        TokenUser tokenUser = TokenUtils.getTokenUser(token);
        return Result.OK(tokenUser);
    }

    @GetMapping("/current-user")
    public Result<CurrentUser> getCurrentUser(@RequestHeader(OaConstants.HEADER_NAME_TOKEN) String token){
        TokenUser tokenUser = TokenUtils.getTokenUser(token);
        return Result.OK(tokenUser);
    }

    //生成用户
    @GetMapping("/new_user")
    public Result newUser(String userId){

         homeService.findEmpName(userId);
         return Result.OK("生成用户成功");
    }


    //t_mgr 主键自增
    private int mgId;
    @PostConstruct  //创建对象后一次性执行的方法
    public void init(){
        mgId = homeService.findMixMgId();
    }

    public synchronized int getMgId(){
        return ++mgId;
    }


    //设为部门主管
    @GetMapping("/leader")
    public Result leader(String userId){
        //t_mgr   e_id d_id
        //前端传来e_id --> t_emp 查d_id --> 判断t_mgr中有无此部门主管,有抛异常,无则添加
        int d_id = homeService.findDepIdByUserId(userId); //查d_id
        int i =  homeService.checkDep(d_id);   //无则返回0
        homeService.deleteMgrByDepId(d_id);    //，有则删除
        homeService.insertMgr(getMgId(),userId,d_id);//--->在checkDep逻辑中执行
        return Result.OK();




    }






}
